package lsl.leetCode.year2023.test0303;

import java.util.Arrays;

/**
 * description:414.第三大的数
 *
 * @author: liushengli
 * data: 2023/3/3
 */
public class test414 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        thirdMax(nums);
    }

    public static int thirdMax(int[] nums) {
        Arrays.sort(nums);
        if (nums.length < 3) {
            return nums[nums.length - 1];
        }
        int count = 1;
        int num = 0;
        int i = 0;
        for (int j = nums.length - 1; j >= 0; j = nums.length - 1 - i) {
            if (count == 3) {
                return num;
            }
            if (j == 0 && count != 3) {
                return nums[nums.length - 1];
            }

            if (nums[j - 1] < nums[j]) {
                num = nums[j - 1];
                count++;
            }
            i++;
        }
        return nums[nums.length - 1];
    }

}
